package com.example.roundcornerprogressbar.view.progressbar.indeterminate;

import com.example.roundcornerprogressbar.view.progressbar.CenteredRoundCornerProgressBar;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.app.Context;
import ohos.app.dispatcher.TaskDispatcher;

public class IndeterminateCenteredRoundCornerProgressBar extends CenteredRoundCornerProgressBar {
    public IndeterminateCenteredRoundCornerProgressBar(Context context) {
        super(context);
    }

    public IndeterminateCenteredRoundCornerProgressBar(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    public IndeterminateCenteredRoundCornerProgressBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    @Override
    protected void initView() {
        super.initView();
        setMax(100);
    }

    @Override
    protected void onViewDraw() {
        TaskDispatcher dispatcher = getContext().getUITaskDispatcher();
        dispatcher.asyncDispatch(new Runnable() {
            @Override
            public void run() {
                int visibility = getVisibility();
                if (visibility == Component.VISIBLE) {
                    startIndeterminateAnimation();
                }
            }
        });
    }

    @Override
    protected void onProgressChangeAnimationUpdate(DirectionalLayout layout, float current, float to) {
        super.onProgressChangeAnimationUpdate(layout, current, to);
        if (!isComponentDisplayed()) {
            super.stopProgressAnimationImmediately();
        }
    }

    @Override
    protected void onProgressChangeAnimationEnd(final DirectionalLayout layout) {
        if (isComponentDisplayed()) {
            startIndeterminateAnimation();
        }
    }

    public void startIndeterminateAnimation() {
        disableAnimation();
        setProgress(0);
        enableAnimation();
        setProgress(100);
    }

    public void stopIndeterminateAnimation() {
        super.stopProgressAnimationImmediately();
    }
}
